package top.raxidin.common.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@Slf4j
@Component
public class LoginIntreceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断session里面是否有用户信息
        Object userInfo = request.getSession().getAttribute("userInfo");
        if(userInfo==null){
            //未登录
            log.debug("拦截请求："+request.getRequestURI());
            response.sendRedirect(request.getContextPath()+"login");
            return false;
        }

        log.debug("放行请求："+request.getRequestURI());
        return true;
    }
}
